import axios from 'axios'
import nProgress from 'nprogress'
import 'nprogress/nprogress.css'

// 配置请求基础URL
// axios.defaults.baseURL = 'http://localhost:8000/';
axios.defaults.baseURL = 'http://192.168.1.8:8001/';
// 请求拦截器
axios.interceptors.request.use(config => {
    // 展示进度条
    nProgress.start();
    // 添加token到请求头
    config.headers.Authorization = window.sessionStorage.getItem('token');
    return config;
}, error => {
    nProgress.done();
    return Promise.reject(error);
});

// 响应拦截器
axios.interceptors.response.use(response => {
    // 隐藏进度条
    nProgress.done();
    return response;
}, error => {
    // 错误响应时也要隐藏进度条
    nProgress.done();
    return Promise.reject(error);
});

export default axios;